package com.xtaller.lazy.api.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xtaller.lazy.base.TApi;
import com.xtaller.lazy.lib.R;
import com.xtaller.lazy.lib.annotation.Permission;
import com.xtaller.lazy.lib.convert.J;
import com.xtaller.lazy.lib.convert.M;
import com.xtaller.lazy.lib.convert.V;
import com.xtaller.lazy.model.system.Module;
import com.xtaller.lazy.parameter.DocModule;
import com.xtaller.lazy.service.system.ModuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @version V1.0
 * @author: Taller
 * @date: 2019-04-09
 * @Description: 模块管理
 */
@Api(tags = "系统模块api")
@RestController
@RequestMapping("/v1/system")
public class ModuleApi extends TApi {
    @Autowired private ModuleService moduleService;
    
    private Map<String, String> baseVerify() {
        var verify = M.create("name", "模块名称不能为空")
                .put("code", "模块编码不能为空");
        return verify;
    }

    @GetMapping("/module")
    @ApiOperation("读取数据")
    @Permission(authorities = "system-module-table")
    public Object table(@ApiParam("页码") @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                      @ApiParam("页长") @RequestParam(value = "limit", required = false, defaultValue = "50") int limit){
        var p = new Page<Module>(page, limit);
        var qw = new QueryWrapper<Module>();
        qw.lambda().orderByDesc(Module::getOrderNumber);
        return p2t(moduleService.page(p, qw));
    }

    @PostMapping("/module")
    @ApiOperation("创建数据")
    @Permission(authorities = "system-module-create")
    public Object create(@ApiParam("数据对象") @RequestBody DocModule object){
        var check = V.checkEmpty(baseVerify(), object);
        if (check.getBoolean(CHECK)) {
            return R.error(check.getString(MESSAGE));
        }
        // 唯一性判断
        var qw = new QueryWrapper<Module>();
        qw.lambda().or(x -> x.eq(Module::getName, object.getName())
                                .or().eq(Module::getCode, object.getCode()));
        var only = moduleService.list(qw);
        if(only.size() > 0) {
            return R.error("模块名称/编码已经存在");
        }
        var model = J.o2m(object, Module.class);
        model.setCreateId(userId.get());

        model = moduleService.model(model);
        if(model == null) {
            return R.error("数据创建失败");
        }
        return R.ok("数据创建成功", model);
    }


    @PutMapping("/module/{id}")
    @ApiOperation("修改数据")
    @Permission(authorities = "system-module-update")
    public Object update(@ApiParam("数据id") @PathVariable Long id, @ApiParam("数据对象") @RequestBody DocModule object){
        var model = moduleService.getById(id);
        if(model == null) {
            return R.error("数据id异常");
        }

        var check = V.checkEmpty(baseVerify(), object);
        if (check.getBoolean(CHECK)) {
            return R.error(check.getString(MESSAGE));
        }
        // 唯一性判断
        var qw = new QueryWrapper<Module>();
        qw.lambda()
                .eq(Module::getCode, object.getCode())
                .ne(Module::getId, id);
        var only = moduleService.list(qw);
        if(only.size() > 0) {
            return R.error("模块名称/编码已经存在");
        }
        model = J.o2m(object, Module.class);
        model.setId(id);
        model.setUpdateId(userId.get());

        model = moduleService.model(model);
        if(model == null) {
            return R.error("数据修改失败");
        }
        return R.ok("数据修改成功", model);
    }

    @DeleteMapping("/module/{id}")
    @ApiOperation("删除数据")
    @Permission(authorities = "system-module-delete")
    public Object delete(@ApiParam("数据id") @PathVariable Long id){
        var model = moduleService.getById(id);
        if(model == null){
            return R.error("数据id异常");
        }
        if(moduleService.removeById(id)) {
            return R.ok("数据删除成功", J.create("id", id));
        }
        return R.error("数据删除失败");
    }
}
